const AllModel = require('./models')
const { Hash,compare,sign,verify} = require('./utils/tools')


//用户登录
const login = async(req,res,next)=>{
    const {username,password} = req.body
    
    let result = await AllModel.findAdmins(username)
    if (result) {
       //验证用户是否为合法用户
        let {password:hash} = result
        let compareResult = await compare(password,hash)
        if (compareResult) {

            const token = sign(username)
            //自定义首部字段向前端传入token
            res.set('X-Access-Token',token)
            res.set('Access-Control-Expose-Headers', 'X-Access-Token')
            res.render('success',{
                data:JSON.stringify({
                    message:"登录成功",
                }),
            })
        } else {
            res.render('fail',{
                data:JSON.stringify({
                    message:"错了笨蛋",
                })
            })
        }
   }else{
        res.render('fail',{
            data:JSON.stringify({
                message:"用户名密码错误",
               
            })
        })
   }
       
}

exports.login = login;
//注册管理员
const zhuce = async (req,res,next)=>{
    res.set('content-type','application/json; charset=utf-8')
    const { username,password,email,position} = req.body
    //密码加密
    const bcryptPassword = await Hash(password)
    //判断用户是否存在
    let findResult = await AllModel.findAdmins(username)
    if(findResult){
        res.render('fail',{
            data:JSON.stringify({
                message:"用户存在"
            })
        })
    }else{
        //数据库没有这个用户则添加这个用户
        let result = await AllModel.zhuceAdmins({
            username,
            password:bcryptPassword,
            email,
            position,
        })

        res.render('success',{
            data:JSON.stringify({
                message:"用户注册已成功"
            })
        })
    }
}
//获取管理员列表
const AdminsList = async(req,res,next)=>{
    res.set('content-type','application/json; charset=utf-8')
    const listResult = await AllModel.findListAdmins()
    res.render('success',{
        data:JSON.stringify(listResult)
    })
}
//模糊搜索管理员
const GrabbleAdmins = async(req,res,next)=>{
    res.set('content-type','application/json; charset=utf-8')
    const {params} = req.body
    const GrabbleUsername = await AllModel.findGrabbleNameAdmins(params)
    if (GrabbleUsername) {
        res.render('success',{
            data:JSON.stringify({
                GrabbleUsername,
                message:"查询成功",
            }),
        })
    }else{
        res.render('fail',{
            data:JSON.stringify({
                message:"查询失败",
               
            })
        })
    }
}
//删除管理员
const deleteAdmins = async(req,res,next)=>{
    res.set('content-type','application/json; charset=utf-8')
    const {id} = req.body
    let result =  await AllModel.removeAdmins(id)
    if (result) {
        res.render('success',{
            data:JSON.stringify({
                message:'用户删除成功'
            })
        })
        
    } else {
        res.render('fail',{
            data:JSON.stringify({
                message:'用户删除失败'
            })
        })
    }
}
// id查询管理员
const ListOneAdmins = async (req,res,next)=>{
    let findResult = await AllModel.findOneAdmins(req.body.id)
    if (findResult) {
        res.json(findResult)
    }else{
        res.render('fail',{
            findResult,
            data:JSON.stringify({
                message:'用户查找失败'
            })
        })
    }
}
//修改管理员
const ModifyAdmins = async (req,res,next)=>{
    res.set('content-type','application/json; charset=utf-8')
    let { id,username,email,position} = req.body;
        let result = await AllModel.upDateAdmins({
            id,
            username,
            email,
            position,
        })
        if(result){
            res.render('success',{
                data:JSON.stringify({
                    id,
                    message:"管理员修改成功"
                })
            })
        }else{
             res.render('fail',{
                data:JSON.stringify({
                    id,
                    message:'管理员修改失败'
                })
            })
        }
        
       
}
exports.AdminsList = AdminsList;
exports.zhuce = zhuce;
exports.GrabbleAdmins = GrabbleAdmins;
exports.deleteAdmins = deleteAdmins;
exports.ListOneAdmins = ListOneAdmins;
exports.ModifyAdmins = ModifyAdmins;

// id查询人事
const ListOne = async (req,res,next)=>{
    let findResult = await AllModel.findOne(req.body.id)
    if (findResult) {
        res.json(findResult)
    }else{
        res.render('fail',{
            findResult,
            data:JSON.stringify({
                message:'用户查找失败'
            })
        })
    }
}
//修改人事
const ModifyUser = async (req,res,next)=>{
    res.set('content-type','application/json; charset=utf-8')
    let { id,username,Tel,role,salary,state} = req.body;
    console.log(id);
        let result = await AllModel.upDate({
            id,
            username,
            Tel,
            role,
            salary,
            state,
        })
        if(result){
            res.render('success',{
                data:JSON.stringify({
                    id,
                    message:"用户修改成功"
                })
            })
        }else{
             res.render('fail',{
                data:JSON.stringify({
                    id,
                    message:'用户修改失败'
                })
            })
        }
        
       
}
//获取人事列表
const UserList = async(req,res,next)=>{
    res.set('content-type','application/json; charset=utf-8')
    const listResult = await AllModel.findList()
    res.render('success',{
        data:JSON.stringify(listResult)
    })
}
//添加人事
const addUser = async (req,res,next)=>{
    res.set('content-type','application/json; charset=utf-8')
    const { username,Tel,role,salary,state} = req.body
    //判断用户是否存在
    let findResult = await AllModel.findUser(username)
    if(findResult){
        res.render('fail',{
            data:JSON.stringify({
                message:"员工存在"
            })
        })
    }else{
        //数据库没有这个房屋则添加这个房屋
        let result = await AllModel.signUsers({
            username,
            Tel,
            role,
            salary,
            state,
        })

        res.render('success',{
            data:JSON.stringify({
                message:"员工添加成功"
            })
        })
    }
}
//模糊搜索人事
const Grabble = async(req,res,next)=>{
    res.set('content-type','application/json; charset=utf-8')
    const {params} = req.body
    const GrabbleUsername = await AllModel.findGrabbleName(params)
    if (GrabbleUsername) {
        res.render('success',{
            data:JSON.stringify({
                GrabbleUsername,
                message:"查询成功",
            }),
        })
    }else{
        res.render('fail',{
            data:JSON.stringify({
                message:"查询失败",
               
            })
        })
    }
}

//删除人事
const deleteUser = async(req,res,next)=>{
    res.set('content-type','application/json; charset=utf-8')
    const {id} = req.body
    let result =  await AllModel.remove(id)
    if (result) {
        res.render('success',{
            data:JSON.stringify({
                message:'用户删除成功'
            })
        })
        
    } else {
        res.render('fail',{
            data:JSON.stringify({
                message:'用户删除失败'
            })
        })
    }
}
exports.addUser = addUser
exports.Grabble = Grabble;
exports.UserList = UserList;
exports.deleteUser = deleteUser;
exports.ModifyUser = ModifyUser;
exports.ListOne = ListOne;

//获取财务列表
const FinanceList = async(req,res,next)=>{
    res.set('content-type','application/json; charset=utf-8')
    const listResult = await AllModel.findListFinance()
    res.render('success',{
        data:JSON.stringify(listResult)
    })
}
const addFinance = async (req,res,next)=>{
    res.set('content-type','application/json; charset=utf-8')
    const { money,amountMoney,department} = req.body
        //数据库没有这个房屋则添加这个房屋
        let result = await AllModel.signFinance({
            money,
            amountMoney,
            department,
        })
        res.render('success',{
            data:JSON.stringify({
                message:"发放成功"
            })
        })
}
//删除财务
const deleteFinance = async(req,res,next)=>{
    res.set('content-type','application/json; charset=utf-8')
    const {id} = req.body
    let result =  await AllModel.removeFinance(id)
    if (result) {
        res.render('success',{
            data:JSON.stringify({
                message:'用户删除成功'
            })
        })
        
    } else {
        res.render('fail',{
            data:JSON.stringify({
                message:'用户删除失败'
            })
        })
    }
}
//模糊搜索
const GrabbleFinance = async(req,res,next)=>{
    res.set('content-type','application/json; charset=utf-8')
    const {params} = req.body
    const GrabbleUsername = await AllModel.findGrabbleNameFinance(params)
    if (GrabbleUsername) {
        res.render('success',{
            data:JSON.stringify({
                GrabbleUsername,
                message:"查询成功",
            }),
        })
    }else{
        res.render('fail',{
            data:JSON.stringify({
                message:"查询失败",
               
            })
        })
    }
}
// id查询财务
const ListOneFinance = async (req,res,next)=>{
    let findResult = await AllModel.findOneFinance(req.body.id)
    if (findResult) {
        res.json(findResult)
    }else{
        res.render('fail',{
            findResult,
            data:JSON.stringify({
                message:'用户查找失败'
            })
        })
    }
}
//修改财务
const ModifyFinance = async (req,res,next)=>{
    res.set('content-type','application/json; charset=utf-8')
    let { id,money,amountMoney,department} = req.body;
    console.log(id);
        let result = await AllModel.upDateFinance({
            id,
            money,
            amountMoney,
            department,
        })
        if(result){
            res.render('success',{
                data:JSON.stringify({
                    id,
                    message:"用户修改成功"
                })
            })
        }else{
             res.render('fail',{
                data:JSON.stringify({
                    id,
                    message:'用户修改失败'
                })
            })
        }
        
       
}
exports.FinanceList = FinanceList;
exports.addFinance = addFinance;
exports.deleteFinance = deleteFinance;
exports.GrabbleFinance = GrabbleFinance;
exports.ListOneFinance = ListOneFinance;
exports.ModifyFinance = ModifyFinance;

//获取设备列表
const EquipmentsList = async(req,res,next)=>{
    res.set('content-type','application/json; charset=utf-8')
    const listResult = await AllModel.findListEquipments()
    res.render('success',{
        data:JSON.stringify(listResult)
    })
}
//添加设备
const addEquipments = async (req,res,next)=>{
    res.set('content-type','application/json; charset=utf-8')
    const { DeviceName,state,PurchaseDate,money,num} = req.body
        //数据库没有这个房屋则添加这个房屋
        let result = await AllModel.signEquipments({
            DeviceName,
            state,
            PurchaseDate,
            money,
            num,
        })
        res.render('success',{
            data:JSON.stringify({
                message:"发放成功"
            })
        })
}
//模糊搜索设备
const GrabbleEquipments = async(req,res,next)=>{
    res.set('content-type','application/json; charset=utf-8')
    const {params} = req.body
    const GrabbleUsername = await AllModel.findGrabbleNameEquipments(params)
    if (GrabbleUsername) {
        res.render('success',{
            data:JSON.stringify({
                GrabbleUsername,
                message:"查询成功",
            }),
        })
    }else{
        res.render('fail',{
            data:JSON.stringify({
                message:"查询失败",
               
            })
        })
    }
}
//删除设备
const deleteEquipments = async(req,res,next)=>{
    res.set('content-type','application/json; charset=utf-8')
    const {id} = req.body
    let result =  await AllModel.removeEquipments(id)
    if (result) {
        res.render('success',{
            data:JSON.stringify({
                message:'删除成功'
            })
        })
        
    } else {
        res.render('fail',{
            data:JSON.stringify({
                message:'删除失败'
            })
        })
    }
}
// id查询设备
const ListOneEquipments = async (req,res,next)=>{
    let findResult = await AllModel.findOneEquipments(req.body.id)
    if (findResult) {
        res.json(findResult)
    }else{
        res.render('fail',{
            findResult,
            data:JSON.stringify({
                message:'查找失败'
            })
        })
    }
}
//修改财务
const ModifyEquipments = async (req,res,next)=>{
    res.set('content-type','application/json; charset=utf-8')
    let { id,DeviceName,state,PurchaseDate,money,num} = req.body;
    console.log(id);
        let result = await AllModel.upDateEquipments({
            id,
            DeviceName,
            state,
            PurchaseDate,
            money,
            num,
        })
        if(result){
            res.render('success',{
                data:JSON.stringify({
                    id,
                    message:"修改成功"
                })
            })
        }else{
             res.render('fail',{
                data:JSON.stringify({
                    id,
                    message:'修改失败'
                })
            })
        }
        
       
}
exports.EquipmentsList = EquipmentsList;
exports.addEquipments = addEquipments;
exports.GrabbleEquipments = GrabbleEquipments;
exports.deleteEquipments = deleteEquipments;
exports.ListOneEquipments = ListOneEquipments;
exports.ModifyEquipments = ModifyEquipments;

//获取客户列表
const ReceptionsList = async(req,res,next)=>{
    res.set('content-type','application/json; charset=utf-8')
    const listResult = await AllModel.findListReceptions()
    res.render('success',{
        data:JSON.stringify(listResult)
    })
}
//模糊搜索客户
const GrabbleReceptions = async(req,res,next)=>{
    res.set('content-type','application/json; charset=utf-8')
    const {params} = req.body
    const GrabbleUsername = await AllModel.findGrabbleNameReceptions(params)
    if (GrabbleUsername) {
        res.render('success',{
            data:JSON.stringify({
                GrabbleUsername,
                message:"查询成功",
            }),
        })
    }else{
        res.render('fail',{
            data:JSON.stringify({
                message:"查询失败",
               
            })
        })
    }
}
//删除客户
const deleteReceptions = async(req,res,next)=>{
    res.set('content-type','application/json; charset=utf-8')
    const {id} = req.body
    let result =  await AllModel.removeReceptions(id)
    if (result) {
        res.render('success',{
            data:JSON.stringify({
                message:'删除成功'
            })
        })
        
    } else {
        res.render('fail',{
            data:JSON.stringify({
                message:'删除失败'
            })
        })
    }
}
//添加客户
const addReceptions = async (req,res,next)=>{
    res.set('content-type','application/json; charset=utf-8')
    const { Customerlist,identity,affair,speedprogress} = req.body
        //数据库没有这个房屋则添加这个房屋
        let result = await AllModel.signReceptions({
            Customerlist,
            identity,
            affair,
            speedprogress,
        })
        res.render('success',{
            data:JSON.stringify({
                message:"添加成功"
            })
        })
}
// id查询客户
const ListOneReceptions = async (req,res,next)=>{
    let findResult = await AllModel.findOneReceptions(req.body.id)
    if (findResult) {
        res.json(findResult)
    }else{
        res.render('fail',{
            findResult,
            data:JSON.stringify({
                message:'查找失败'
            })
        })
    }
}
//修改客户
const ModifyReceptions = async (req,res,next)=>{
    res.set('content-type','application/json; charset=utf-8')
    let { id,Customerlist,identity,affair,speedprogress} = req.body;
        let result = await AllModel.upDateReceptions({
            id,
            Customerlist,
            identity,
            affair,
            speedprogress,
        })
        if(result){
            res.render('success',{
                data:JSON.stringify({
                    id,
                    message:"修改成功"
                })
            })
        }else{
             res.render('fail',{
                data:JSON.stringify({
                    id,
                    message:'修改失败'
                })
            })
        }
        
       
}
exports.ReceptionsList = ReceptionsList;
exports.GrabbleReceptions = GrabbleReceptions;
exports.deleteReceptions = deleteReceptions;
exports.addReceptions = addReceptions;
exports.ListOneReceptions = ListOneReceptions;
exports.ModifyReceptions = ModifyReceptions;